﻿using System.Collections.Generic;
using Slaks.Graphs.GraphBuilder;
using Slaks.Graphs.Graphs;
using System.Diagnostics;
namespace Slaks.Graphs.Algorithms
{
    public class DistanceAlgFactory:FactoryBase<GraphDistanceBase>
    {
        private static DistanceAlgFactory s_instance;
        public static DistanceAlgFactory Instance
        {
            get
            {
                if (s_instance == null)
                {
                    s_instance = new DistanceAlgFactory();
                }
                return s_instance ;
            }
        }
        public string[] GetSuitableDistanceAlgorithms(BuilderType BT)
        {
            var algs = new List<string>();
            Debug.WriteLine(string.Format("Looking for distance algorithms for {0}", BT));
            
            var distances = GetAvailableAlogrithms();
            foreach (var distance in distances)
            {
                if (distance.IsVisible && distance.CanOperateOnRepresentation(BT))
                {
                    algs.Add(distance.GetName());
                }
                Debug.WriteLine(string.Format("Distance {0} is visible? {1} can operate? {2}", distance, distance.IsVisible, distance.CanOperateOnRepresentation(BT)));
            }
            Debug.WriteLine(string.Format("total found: {0}", algs.ToArray().Length));
            return algs.ToArray();
        }

        public string [] GetSuitableDistanceAlgorithms ( IRepresentation representation )
        {
            return GetSuitableDistanceAlgorithms ( representation.getBuilderType ());
        }

        protected override GraphDistanceBase FindClass(string key, IEnumerable<GraphDistanceBase> algorithms)
        {
            foreach (var distAlg in algorithms)
            {
                if (distAlg.GetName().Equals(key))
                {
                    return distAlg;
                }

            }
            return null;
        }
    }
}
